/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  Date: 23/Jan/2014
//  E-Mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************

#define SK_COMMON_C_
#include <stdio.h>
#include "N79E81x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "adc.h"
#include "main.h"
#include "AP_Data.h"
#include "Dh11.h"
#define MORE_TEMPVAL  1
#define  MAX_TEMP  200
#define   MIN_TEMP  80
	//UINT16 code TEMP_VAL[]={1010,1005,1000,950,910,870,821,761,703,575,510,434,366,302,244,198,169,138,116,93,74,62,51,41};
	//UINT16 code TEMP_VAL2[]={1010,1005,1000,975,950,940,910,890,870,800,821,790,761,730,703,690,575,540,510,470,434,400,366,330,302,270,244,220,198,180,169,150,138,130,116,105,93,80,74,70,62,55,51,46,41};  //��� 250		   	,371,338ɾ��   391,381	   160 150

UINT16 code TEMP_VAL3[]={785,681,568,455,354,265,198,148,109,84,64};  //������¿�0-100
//UINT16  mode_time[]={55,65,75,85,95};
//UINT16  mode_time[]={95,85,75,65,55,45,35};
UINT8  code Interval_time[][2]={{10,0},{20,10},{20,20},{10,10},{5,5}};   //{wordtime,pausetime}  ?????????,??X100ms
UINT16 code strong_value[]={1023,800,600,500,400,300,200,0};//pwm ??????
UINT8 CYCLE=100;
UINT8 wait_time;
UINT8 smart_wait_time;  //??????
static UINT16 check_time;
UINT8 TH_data,TL_data,RH_data,RL_data,CK_data;
UINT8 TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
UINT8 showtemp,com_data,respond,temp,untemp;
void InitialUART0_Timer1(UINT32 u32Baudrate)
{
    SCON = 0x52;
    TMOD = 0x20;
    TH1 = 256 - (57600/u32Baudrate); /* 22.1184M/384=57?600 */
    TR1 = 1;
}

void ADC_CHECK(){
	UINT16 u16ADCL;
	ADC_Channel_Sel(E_CHANNEL2);//p03
	Trigger_ADC_Convertion();  //??????CPU
    u16ADCL = ADCCON0;
    u16ADCL = u16ADCL >> 6;                         // ADC[1:0]
    u16ADC = ADCH;
    u16ADC = (u16ADC << 2 ) + u16ADCL;              // ADC[9:2] + ADC[1:0]
	
}
  void TEMP_CHECK(){
	UINT16 u16ADCL;
		UINT8 I,TEMP,TEMP_G;
	ADC_Channel_Sel(E_CHANNEL1);	 //P04
	Trigger_ADC_Convertion();  //??????CPU
    u16ADCL = ADCCON0;
    u16ADCL = u16ADCL >> 6;                         // ADC[1:0]
    u16ADC = ADCH;
    u16ADC = (u16ADC << 2 ) + u16ADCL;              // ADC[9:2] + ADC[1:0]	
	for( I=0;I<11;I++){
			if(u16ADC<=TEMP_VAL3[I]&&u16ADC>TEMP_VAL3[I+1])
			{
				break;
			}
		}
	
	 TEMP = 10*I;
	 TEMP_G =  ((TEMP_VAL3[I]-u16ADC)*10)/(TEMP_VAL3[I]-TEMP_VAL3[I+1]);
	 now_temp= TEMP + TEMP_G;
}



void CHECK_WORK(){	
   static UINT8 time_cnt;
   UINT8 work_time,pause_time;
   time_cnt++;
   work_time = Interval_time[MODE_INDEX][0];
   pause_time = Interval_time[MODE_INDEX][1];
//   if (time_cnt==work_time){
//	  if(pause_time!=0) set_PWM_val(1023);//电机停止
//		 else{set_PWM_val(strong_value[STRONG_INDEX]);}//持续震动
//   }else if(time_cnt>=(work_time+pause_time))// ??????,????
//	  { 
//			time_cnt = 0;
//	    set_PWM_val(strong_value[STRONG_INDEX]);
//   }

}




//??ad???  CH-:100R 10 ; power ;1k = 133  ;4.7= 327 ; 6.8K= 414 ;10K = 512

//??ad???  124 321 932 682 507

void KEY_CHECK(){
	static UINT8  key_code=0;
	static BIT key_realse=0;
	static BIT have_key=0;
	static UINT16 key_on_cnt;
	    ADC_CHECK();
		if(key_realse==1&&u16ADC<980){
		    if(have_key == 0){
			have_key = 1;
			return;
		    }
		
		}
		 if(u16ADC>0&&u16ADC<30){
			key_code = 3;//��
		} 
		else if(u16ADC>100&&u16ADC<180){
			key_code = 2;//��
			
		} else if(u16ADC>580&&u16ADC<650){
			key_code = 1;//
			
		}else if(u16ADC>740&&u16ADC<820){
			key_code = 4;//
			
		}else if(u16ADC>980)//????
		{
		  if(key_code==2){//2???

		  }
		  key_code = 0;
		  key_on_cnt=0;//??????
		  key_realse=1;
		  have_key = 0;
		  
		}  
	
		switch(key_code){
			case 1://mode  ????
			if(key_realse==1)
			{
			   MODE_INDEX++;
			   if(MODE_INDEX>=5){
				   MODE_INDEX = 0;
			   }
				
			}
			key_realse=0;
			break;  
			case 2://??
				if(key_realse==1){
			
				 if(STRONG_INDEX>0) STRONG_INDEX-- ;
					 set_PWM_val(strong_value[STRONG_INDEX]);
				}
				else {
					if(key_on_cnt<300)key_on_cnt++;
					else {
					
					}
				}
				key_realse=0;
			break;
			case 3://??
			if(key_realse==1)
			{
				STRONG_INDEX++;
				if(STRONG_INDEX>=7) STRONG_INDEX = 7;
				set_PWM_val(strong_value[STRONG_INDEX]);
			}
			key_realse=0;
			break;
			case 4://?????
			if(key_realse==1){
			SHOW_MODE++;
			if(SHOW_MODE>2) SHOW_MODE=0;	
				 
			}
			key_realse=0;
			break;
	
			case 0:  //????
			break;
				
			}
	
}
//-----------------------------------------------------------------------------------------------------------
UINT8 Receive_Data_From_PC(void)
{
    UINT8 c;
    while (!RI);
    c = SBUF;
    RI = 0;
    return (c);
}
//-----------------------------------------------------------------------------------------------------------
void Send_Data_To_PC (UINT8 c)
{
    while (!TI);
    TI = 0;
    SBUF = c;
}



void get_config(){
	Get_DATA2((UINT8*) &myconfig,0x00,sizeof(config));
	if (myconfig.target_temp == 0xffff){//??????
	   myconfig.target_temp = 35;
	   myconfig.target_temp_min = 27;
	   myconfig.COMPRESSOR_BEFOR_TIME = BEFOR_TIME;
	}
}

void save_config(){
  Write_DATA2((UINT8*) &myconfig,0x00,sizeof(config));
}
void init_state(){//???????
	Clear_BOF();
    get_config();

	MODE_INDEX = 0;
    STRONG_INDEX = 0;
}

void Function_1sProcess(void)
{
 UINT8 CNT;
	if (sk_1s_flag==0)
			return;
		else sk_1s_flag=0;

				
			 SOUND_CONTROL=1;         //拉低总线
				 Delay10us(1);	
				SOUND_CONTROL=0;
	            CNT= 2;
			
				//while(!DATA_PIN&&CNT++){}//等待电平边高
				//check_state=1;
				//can_check_flag=0;
				 getDistance ();
      
     	 
}


void Function_500msProcess(void)
{
 	if (sk_500ms_flag==0)
 		return;
 	else sk_500ms_flag=0;
	SHOW_FALG=!SHOW_FALG;////????
//	TEMP_CHECK();
//	read_TRH();

}


void Function_50msProcess(void)
{		static UINT8 TIME_CNT;
		if (sk_50ms_flag==0)
			return;
		else sk_50ms_flag=0;
	   if(TIME_CNT++>=2){//100ms
		 TIME_CNT=0;
		CHECK_WORK();//????
		}
}

void Function_10msProcess(void)
{
//	static UINT8 TIME_CNT;
		if (sk_10ms_flag==0)
			return;
		else sk_10ms_flag=0;
		KEY_CHECK();//????

}
void Function_1msProcess(void)
{		static UINT8 cnt=0,TIME_CNT;

		if (sk_1ms_flag==0)
			return;
		else sk_1ms_flag=0;
		if(cnt>=1){
		    if (SHOW_MODE==0)
		    show_screen(STRONG_INDEX,0);  //????
			else if (SHOW_MODE==1){
				show_screen(now_temp,1);//????
			}else if(SHOW_MODE==2){
			show_screen(distance,2);//????
			}
		}
		cnt++;
	
		
}



#undef SK_COMMON_C_
//-----------------------------------------------------------------------------------------------------------
